package org.liujing.jsw.client.ui;

import com.google.gwt.user.client.ui.*;
import org.liujing.jsw.client.border.*;
import org.liujing.jsw.client.ui.plaf.*;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.*;
import com.google.gwt.event.shared.*;
import com.google.gwt.event.dom.client.*;
import java.util.*;

public class JComponent extends Container{

	protected Widget gwtPeer; // used internally for utilized GWT event handling mechanism.
	
	public JComponent(){
		updateUI();
		gwtPeer = new GWTWidgetWrapper(this);
	}
	
	protected <H extends EventHandler> HandlerRegistration addDomHandler(H handler, DomEvent.Type<H> type){
		gwtPeer.addDomHandler(handler, type);
	}
    
    public void setUI(ComponentUI newUI) {
        if (ui != null) {
            ui.uninstallUI(this);
        }
        
        ComponentUI oldUI = ui;
        ui = newUI;
        if (ui != null) {
            ui.installUI(this);
        }

        //firePropertyChange("UI", oldUI, newUI);
        //revalidate();
        //repaint();
    }
    
    	public void setBorder(Border border) {
		Border oldBorder = this.border;
	
		this.border = border;
		//firePropertyChange("border", oldBorder, border);
		if (border != oldBorder) {
			if(oldBorder != null)
				oldBorder.removeBorder(this);
		    if (border == null || oldBorder == null ||
			!(border.getBorderInsets(this).equals(oldBorder.getBorderInsets(this)))) {
			revalidate();
		    } 
		    getBorder().createBorder(this, 0, 0, width, height);
		}
	}
    
	public Border getBorder() {
		return border;
	}
	
	public void revalidate() {
		if (getParent() == null) {
		    return;
		}
		//if (SwingUtilities.isEventDispatchThread()) {
		invalidate();
		//RepaintManager.currentManager(this).addInvalidComponent(this);
		RepaintManager.addInvalidComponent(this);
		//}
	}
    
    	public void updateUI() {
		setUI(UIManager.getUI(this));
	}
	
	@Override
	public void onload(){
		ui.onComponentLoad(this);
	}
    
	public enum BaselineResizeBehavior {
	CONSTANT_ASCENT,
	CONSTANT_DESCENT,
	CENTER_OFFSET,
	OTHER
	}
}
